package com.mtoolkit.jdbc.handler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Object list result set call back handler.
 *
 * @author 	<a href="mailto:shixi.zhang@f-club.com">ZhangShixi</a>
 * @version 1.00, 5/21/2012
 * @since 	JDK1.5
 */
public class ListResultSetHandler implements ResultSetHandler<List<Object>> {

	/**
     * Handles the result set and return object list result.
     * 
     * @param  rs result set.
     * 
     * @return object array result.
     * 
     * @throws SQLException if a sql error occurs whiling handle result set.
     */ 
	@Override
	public List<Object> handle(ResultSet rs) throws SQLException {
		if (!rs.next()) {
            return null;
        }

        ResultSetMetaData meta = rs.getMetaData();
        int columnCount = meta.getColumnCount();
        List<Object> results = new ArrayList<Object>(columnCount);

        for (int i = 0; i < columnCount; i++) {
            results.add(rs.getObject(i + 1));
        }

        return results;
	}

}
